package main

import(
	assetfs "github.com/elazarl/go-bindata-assetfs"
	"gitee.com/jinmingzhi/rest_test/static"
	"github.com/grpc-ecosystem/grpc-gateway/runtime"
	"google.golang.org/grpc"
	pb "gitee.com/jinmingzhi/rest_test/api"
	"context"
	"go.uber.org/zap"
	"net/http"
)

var (
	PublicHandler *Public
)

func Initglobal(){
	PublicHandler = new(Public)
}

func RegisterGRPC(s *grpc.Server) {
	pb.RegisterPublicServer(s, PublicHandler)
}

func ServerSwaggerUi(mux *http.ServeMux){
	fileServer := http.FileServer(&assetfs.AssetFS{
		Asset:    static.Asset,
		AssetDir: static.AssetDir,
		Prefix:   "swagger",
	})
	prefix := "/ui/"
	mux.Handle(prefix, http.StripPrefix(prefix, fileServer))
}

func MustRegisterREST(mux *runtime.ServeMux, grpcListAddress string){
	if err := pb.RegisterPublicHandlerFromEndpoint(context.Background(), mux, "localhost"+grpcListAddress, []grpc.DialOption{grpc.WithInsecure()}); err != nil {
		log.Fatal("register public email rest error", zap.Error(err))
	}
}
